package org.jeecg.modules.yks.mng.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.math3.geometry.partitioning.BSPTree;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.DictModel;
import org.jeecg.modules.yks.mng.entity.YksEquipmentDocument;
import org.jeecg.modules.yks.mng.entity.YksEquipmentManageRundata;


import org.jeecg.modules.yks.mng.service.IYksEquipmentManageRundataService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Optional;

@Api(tags="设备运行管理")
@RestController
@RequestMapping("/mng/yksEquipmentManageRundata")
@Slf4j
public class YksEquipmentManageRundataController extends JeecgController<YksEquipmentManageRundata, IYksEquipmentManageRundataService> {
    @Autowired
    private IYksEquipmentManageRundataService yksEquipmentManageRundataService;
    @Autowired
    private ISysBaseAPI sysBaseAPI;
    /**
     * 分页列表查询
     *
     * @param yksEquipmentManageRundata
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "消息表-分页列表查询")
    @ApiOperation(value="消息表-分页列表查询", notes="消息表-分页列表查询")
    @GetMapping(value = "/list")
    public Result<List<YksEquipmentManageRundata>> queryPageList(YksEquipmentManageRundata yksEquipmentManageRundata,
                                                                  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                                  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                                                  @RequestParam(name="equipmentId") String equipmentId,
                                                                  HttpServletRequest req) {
        QueryWrapper<YksEquipmentManageRundata> queryWrapper = QueryGenerator.initQueryWrapper(yksEquipmentManageRundata, req.getParameterMap());

        List<YksEquipmentManageRundata> pageList =yksEquipmentManageRundataService.queryYksEquipmentManageRundataByEquipmentId(equipmentId); //yksEquipmentManageRundataService.page(page, queryWrapper);
        List<DictModel> groups= sysBaseAPI.getDictItems("parameter_group");
        List<DictModel> dataTypes= sysBaseAPI.getDictItems("parameter_data_type");
        List<DictModel> plcModels= sysBaseAPI.getDictItems("plc_model");
        List<DictModel> readandwriteTypes= sysBaseAPI.getDictItems("parameter_readandwrite_type");

        if(!pageList.isEmpty()){
            pageList.forEach(p->{
//                Optional<DictModel> group=groups.stream().filter(q->q.getValue().equals(p.getParameterFirstGroup())).findFirst();
//                group.ifPresent( p.setParameterFirstGroup_DictText(group.get().getText()));


                groups.stream().filter(q->q.getValue().equals(p.getParameterFirstGroup())).findFirst().ifPresent(value->p.setParameterFirstGroup_DictText(value.getText()));
                groups.stream().filter(q->q.getValue().equals(p.getParameterSecondGroup())).findFirst().ifPresent(value->p.setParameterSecondGroup_DictText(value.getText()));
//                p.setParameterSecondGroup_DictText(groups.stream().filter(q->q.getValue().equals(p.getParameterSecondGroup())).findFirst().get().getText());
                dataTypes.stream().filter(q->q.getValue().equals(p.getDataType())).findFirst().ifPresent(value->p.setDataType_DictText(value.getText()));
                plcModels.stream().filter(q->q.getValue().equals(p.getPlcModel())).findFirst().ifPresent(value->p.setPlcModel_DictText(value.getText()));
                readandwriteTypes.stream().filter(q->q.getValue().equals(p.getReadWrite())).findFirst().ifPresent(value->p.setReadWrite_DictText(value.getText()));
            });
        }
        return Result.OK(pageList);
    }

    @PutMapping("/immediateSaveRow")
    public Result immediateSaveRow(@RequestBody YksEquipmentManageRundata rowData) throws Exception {
//        System.out.println("即时保存.rowData：" + rowData.toJSONString());
        // 延时1.5秒，模拟网慢堵塞真实感
//        Thread.sleep(500);
        yksEquipmentManageRundataService.updateIsRevise(rowData);
        return Result.ok();
    }
}
